package accessory.crtm;

import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

import editor.view.MapOrientation;
import editor.view.mousehandling.MouseOverHandler;

import map.GameMap;
import map.RasterCell;
import map.RasterbasedMap;

public class FileHandler {
	
	public RasterCell[][] readOldMapFile(String fileName, MouseOverHandler handler){
		RasterCell[][] stock = null;
		
		try {

			FileInputStream file = new FileInputStream(fileName);
			ObjectInputStream o = new ObjectInputStream(file);
			Dimension dim = (Dimension) o.readObject();
			System.out.println("map size: "+dim);

			stock = new RasterCell[new Double(dim.getWidth()).intValue()][new Double(dim.getHeight()).intValue()];

			for (int i = 0; i < dim.getWidth(); i++) {
				for (int j = 0; j < dim.getHeight(); j++) {
					RasterCell cell = (RasterCell) o.readObject();
					stock[i][j] = cell;
					if(handler != null){
						stock[i][j].addMouseOverHandler(handler);
					}
					stock[i][j].getAttachedObject().deseriralize();
					stock[i][j].getTerrain().deseriralize();										
				}
			}
			
			for (int i = 0; i < dim.getWidth(); i++) {
				for (int j = 0; j < dim.getHeight(); j++) {
					RasterCell cell = stock[i][j];					
							
					if(i > 0){
						stock[i - 1][j].getNeighbors().put(MapOrientation.RIGHT, cell);
					}
					if(j > 0){
						stock[i][j - 1].getNeighbors().put(MapOrientation.BOTTOM, cell);
					}		
					if((i + 1) < dim.getWidth()){
						stock[i + 1][j].getNeighbors().put(MapOrientation.LEFT, cell);
					}
					if((j + 1) < dim.getHeight()){
						stock[i][j + 1].getNeighbors().put(MapOrientation.TOP, cell);
					}				
				}
			}
			
			System.out.println("loaded "+stock.length+" lines");
			o.close();
		} catch (IOException e) {
			System.err.println(e);
		} catch (ClassNotFoundException e) {
			System.err.println(e);
		}
				
		//rasterMap.set
		return stock;
	}
	
	public GameMap readMapFile(String fileName, MouseOverHandler handler){
		GameMap rasterMap = null;

		try{
			FileInputStream file = new FileInputStream(fileName);
			ObjectInputStream o = new ObjectInputStream(file);
			rasterMap = (GameMap) o.readObject();			
			o.close();
			
			Dimension dim = rasterMap.getDimension();
			RasterCell[][] stock = rasterMap.getMapRaster();
			
			for (int i = 0; i < dim.getWidth(); i++) {
				for (int j = 0; j < dim.getHeight(); j++) {
					RasterCell cell = (RasterCell) o.readObject();
					stock[i][j] = cell;
					if(handler != null){
						stock[i][j].addMouseOverHandler(handler);
					}
					stock[i][j].getAttachedObject().deseriralize();
					stock[i][j].getTerrain().deseriralize();										
				}
			}
			
			for (int i = 0; i < dim.getWidth(); i++) {
				for (int j = 0; j < dim.getHeight(); j++) {
					RasterCell cell = stock[i][j];					
							
					if(i > 0){
						stock[i - 1][j].getNeighbors().put(MapOrientation.RIGHT, cell);
					}
					if(j > 0){
						stock[i][j - 1].getNeighbors().put(MapOrientation.BOTTOM, cell);
					}		
					if((i + 1) < dim.getWidth()){
						stock[i + 1][j].getNeighbors().put(MapOrientation.LEFT, cell);
					}
					if((j + 1) < dim.getHeight()){
						stock[i][j + 1].getNeighbors().put(MapOrientation.TOP, cell);
					}				
				}
			}
			
		} catch (IOException e) {
			System.err.println(e);
		} catch (ClassNotFoundException e) {
			System.err.println(e);
		}
				
		//rasterMap.set
		return rasterMap;
	}

}
